package com.inspector.app.ui.chat.holder;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;

import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.inspector.app.BaseApp;
import com.inspector.app.R;
import com.inspector.app.ui.chat.adapter.ChatAdapter;
import com.inspector.app.ui.chat.entity.ReceiveMessagesEntity;
import com.inspector.app.ui.widget.CircleImageView;
import com.inspector.app.ui.chat.utils.DateUtil;

import org.json.JSONException;
import org.json.JSONObject;

public class ItemTextReceiveHolder extends RecyclerView.ViewHolder  {
    private TextView itemTime;
    private TextView name;
    private TextView contentText;
    private CircleImageView itemHeader;
    private RelativeLayout itemLayout;
    private TextView messageRetraction;
    private ChatAdapter.OnItemListener mListener;
    private int position;

    private ReceiveMessagesEntity mEntity;

    public ItemTextReceiveHolder(@NonNull View itemView) {
        super(itemView);
        itemTime = itemView.findViewById(R.id.item_tv_time);
        name = itemView.findViewById(R.id.name);
        contentText = itemView.findViewById(R.id.chat_item_content_text);
        itemHeader = itemView.findViewById(R.id.chat_item_header);
        itemLayout = itemView.findViewById(R.id.item_layout);
        messageRetraction = itemView.findViewById(R.id.message_retraction);
    }

    public void setOnItemListener(ChatAdapter.OnItemListener listener, int position) {
        this.mListener = listener;
        this.position = position;
    }

    public void bind(ReceiveMessagesEntity entity) {
        if (entity != null) {
            if (entity.gettShow() == 1) {
                itemLayout.setVisibility(GONE);
                itemTime.setVisibility(GONE);
                messageRetraction.setVisibility(VISIBLE);
            } else {
                mEntity = entity;
                itemLayout.setVisibility(VISIBLE);
                itemTime.setVisibility(VISIBLE);
                messageRetraction.setVisibility(GONE);
                itemTime.setVisibility(entity.getCreateTime() == 0 ? GONE : VISIBLE);



                String standardTimes = DateUtil.getStandardTimes(entity.getCreateTime());
                itemTime.setText(standardTimes);

                String position = "";
                int source = entity.getSource();
                switch (source) {
                    case 1:
                        position = "指挥员";
                        break;
                    case 2:
                        position = "飞行员";
                        break;
                    case 3:
                        position = "观察员";
                        break;
                }

                name.setText(entity.getSendUserName() + "(" + position + ")");


                Glide.with(BaseApp.getInstance())
                        .load(entity.getSendHeadPic())
                        .apply(new RequestOptions().circleCrop())
                        .into(itemHeader);


                String message = entity.getMessage();
                try {
                    JSONObject jsonObject = new JSONObject(message);
                    String content = jsonObject.getString("content");
                    contentText.setText(content);
                } catch (JSONException e) {

                }

            }


        }

    }


}
